package com.approval.approvalapp.center.acitivty;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.MyAppliaction;
import com.approval.approvalapp.R;
import com.approval.approvalapp.center.adapter.ExceptionReviseAdapter;
import com.approval.approvalapp.center.bean.ConsultCensusBean;
import com.approval.approvalapp.center.bean.ExceptionReviseBean;
import com.approval.approvalapp.myself.activity.WdkqActivity;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExceptionReviseActivity extends BaseActivity implements ExceptionReviseAdapter.ClickListenrer {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    List<ExceptionReviseBean> mList = new ArrayList<>();
    private ExceptionReviseAdapter adapter;
    private String kstimestr = "";//上午时间戳
    private String jstimestr = "";//下午时间错
    private ProgressDialog progressDialog;


    @Override
    public int getLayoutId() {
        return R.layout.exception_revise_layout;
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ExceptionReviseAdapter(mList, this, this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void initData() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在请求");
        progressDialog.show();

        getData();
    }

    public void getData() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orgcode", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.ORG_CODE, "-1"));
        jsonObject.put("areacode", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.REGION_CODE, "-1"));
        jsonObject.put("ycxz_num", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.REGION_CODE, "-1"));
        jsonObject.put("is_select", getIntent().getStringExtra("is_select"));
//        jsonObject.put("is_select", "2");

        Log.d("ExceptiRevisddeActivity", jsonObject.toString());


        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/abnormal_correction",
                jsonObject.toString(), new NetDownResponse() {

                    @Override
                    public void success(String str) {
                        Log.d("ExceponReviseActivity", str);
                        Gson gson = new Gson();
                        Type type = new TypeToken<List<ExceptionReviseBean>>() {
                        }.getType();
                        List<ExceptionReviseBean> list = gson.fromJson(str, type);
                        if (list != null) {
                            mList.clear();
                            mList.addAll(list);
                            adapter.notifyDataSetChanged();
                        } else {
                            mList.clear();
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {
                        if (swipeRefreshLayout != null) {
                            swipeRefreshLayout.setRefreshing(false);
                            if (progressDialog == null){
                                return;
                            }
                            progressDialog.dismiss();
                        }
                    }
                });

    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void click(int index, ExceptionReviseBean bean) {
        new XPopup.Builder(ExceptionReviseActivity.this)
                .autoOpenSoftInput(true)
                .asCustom(new CustomPopup(ExceptionReviseActivity.this, index, bean))
                .show();
    }

    private class CustomPopup extends CenterPopupView {

        ExceptionReviseBean bean;
        int positin;

        public CustomPopup(@android.support.annotation.NonNull Context context, int positin, ExceptionReviseBean bean) {
            super(context);
            this.bean = bean;
            this.positin = positin;
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_popup_new_new;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
            ImageView close = findViewById(R.id.close);
            LinearLayout zsqd_ll = findViewById(R.id.zsqd_ll);
            final TextView tvtime1 = findViewById(R.id.tvtime1);
            LinearLayout wsqd_ll = findViewById(R.id.wsqd_ll);
            final TextView tvtime2 = findViewById(R.id.tvtime2);
            TextView et = findViewById(R.id.et);
            LinearLayout agree_tv = findViewById(R.id.agree_tv);

            et.setText(bean.getQKSM());
            tvtime1.setText(bean.getZaoShangQD());
            tvtime2.setText(bean.getXiWuQD());

            close.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

            zsqd_ll.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    TimePickerView timePickerView = new TimePickerBuilder(ExceptionReviseActivity.this, new OnTimeSelectListener() {
                        @Override
                        public void onTimeSelect(Date date, View v) {
                            long kstime = date.getTime() / 1000;
                            kstimestr = kstime + "";

                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                            tvtime1.setText(simpleDateFormat.format(date));
                        }
                    }).setType(new boolean[]{true, true, true, true, true, false})
                            .setTitleText("选择时间").setRangDate(null, Calendar.getInstance()).build();
                    timePickerView.show();

                }
            });

            wsqd_ll.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    TimePickerView timePickerView = new TimePickerBuilder(ExceptionReviseActivity.this, new OnTimeSelectListener() {
                        @Override
                        public void onTimeSelect(Date date, View v) {
                            long jstime = date.getTime() / 1000;
                            jstimestr = jstime + "";

                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                            tvtime2.setText(simpleDateFormat.format(date));
                        }
                    }).setType(new boolean[]{true, true, true, true, true, false})
                            .setTitleText("选择时间").build();
                    timePickerView.show();
                }
            });

            agree_tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("ID", bean.getID());
                    jsonObject.put("uid", SpUtil.getString(ExceptionReviseActivity.this, SpUtil
                            .USER_RUN_ID, ""));
                    jsonObject.put("AreaCode", SpUtil.getString(ExceptionReviseActivity.this,
                            SpUtil.REGION_CODE, ""));
                    jsonObject.put("YSJL", bean.getState());
                    jsonObject.put("QDtime", bean.getQDtime());
                    jsonObject.put("ZaoShang", bean.getZaoShangQD());
                    jsonObject.put("XiaWu", bean.getXiWuQD());
                    jsonObject.put("WeiChis", bean.getWeiChiS());
                    jsonObject.put("TORG", bean.getOrgCode() == null ? "" :bean.getOrgCode() );
                    jsonObject.put("ZaoShangQD", kstimestr);
                    jsonObject.put("XiaWuQD", jstimestr);
                    Log.d("CustomPopup", jsonObject.toString());
                    NetRequsetUtil.getInstance().netRequestPostJson("v1/center/correct", jsonObject.toString(), new NetDownResponse() {
                        @Override
                        public void success(String str) {
                            Toast.makeText(ExceptionReviseActivity.this, "修正完成", Toast.LENGTH_SHORT).show();

                            mList.remove(positin);
                            adapter.setmList(mList);
                            dismiss();
                        }

                        @Override
                        public void errowithresponse(String str) {
                            ErrorBeanUtil.getErrorMessage(str);
                            dismiss();
                        }

                        @Override
                        public void erro() {

                        }

                        @Override
                        public void finish() {

                        }
                    });
                }
            });

        }

    }
}
